#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data time : 2022/9/16   6:43 下午
# project   : public_Method.py
# IDE       : PyCharm
# email     :17600960626@163.com

import uiautomator2 as u2
import time
from lib.init_data import get_data
import os
from lib.func import think_time
BASE_CONF = get_data(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config.ini"))['App']


class AppPage():
    def __init__(self,d):
        self.d = d
        self.app = BASE_CONF['PAGE_NAME']
        self.time_out = BASE_CONF['WAIT_TIMEOUT']
        self.lanuch_activityl = BASE_CONF['LANUCH_ACTIVITYl']
        self.s = u2.connect()


class BasePage(AppPage):
    def __init__(self, driver):
        self.driver = driver
        AppPage.__init__(self,driver)
        # self.init_app()
        self.s.exists()


#------------------------系统原生方法二次封装--------------------
    def home(self):
        self.d.press('home')

    def get_device_info(self):
        """
        获取设备信息
        :return:
        """
        return self.d.info

    def init_app(self):
        '''启动默认app--小艺输入法'''
        self.d.app_start(self.app)

    def star_app(self,app):
        '''启动某个app，app=包名'''
        self.d.app_start(app)
        time.sleep(2)

    def out_app(self,app):
        '''停止app'''
        self.d.app_stop(app)



    def find_text(self, ele):
        '''查询文本'''
        return self.d(text = ele)


    @think_time
    def find_id(self, ele):
        '''#查找id'''
        return self.d(resourceId=ele)


    def find_xpath(self,ele):
        '''#查找xpath'''
        return self.d.xpath(ele)


    def gettitle_text(self, ele):
        '''#根据UI元素的文本属性查找'''
        return ele.get_text()


    def input_text(self,ele, text):
        '''#根据元素输入'''
        ele.set_text(text)


    def send_click(self,text):
        '''#调起原生输入'''
        self.d.send_keys(text, clear=True)

    @think_time
    def back(self):
        '''#按返回键'''
        self.d.press("back")

    def return_back(self):
        '''返回俩次'''
        a=0
        while a<2:
            self.back()
            a+=1

    def loop_back(self,text):
        '''循环返回直到出现对应数据停止'''
        while not self.find_text(text):
            self.d.press("back")
            time.sleep(0.3)

    def long_clicks(self, x, y, duration=5.0):
        '''
        长按点击
        :param x, y: 坐标
        :param duration: 持续时间
        :param timeout: 超时时间
        :return:
        '''
        self.d.long_click(x,y,duration=duration)

#———————————————————点击、滑动、捕获totas、登录、退出等方法———————————————————————————————————————#


    @think_time
    def text_click(self,ele):
        '''#根据文本查找点击'''
        self.d(text=ele).click()

    @think_time
    def id_click(self, id):
        '''#根据id查找点击'''
        self.find_id(id).click()


    @think_time
    def xpath_click(self,el):
        '''#根据xpath查找点击'''
        self.d.xpath(el).click()

    # @think_time
    def x_y_click(self,x,y):
        '''坐标点击'''
        self.d.click(x,y)
        time.sleep(1)

    @think_time
    def get_text_text(self,text):
        '''#根据文本查找文本'''
        return self.gettitle_text(self.find_text(text))


    @think_time
    def get_xpath_id(self,id):
        '''#根据xpath查找id'''
        return self.find_xpath(self.find_id(id))

    @think_time
    def get_xpath_xpath(self,xpath):
        '''#根据xpath查找xpath'''
        return self.find_xpath(self.find_xpath(xpath))

    @think_time
    def get_id_id(self, id):
        '''#根据id查找文本'''
        return self.gettitle_text(self.find_id(id))


    def input_login(self,text,id):
        '''#输入数据'''
        self.input_text(self.find_id(id), text)


    def msg(self):
        '''#捕获toast提示中的message'''
        mssa=self.d.toast.get_message()
        print(mssa)
        return mssa


    def backif(self,text):
        '''#查询文本，如果没有就返回'''
        if self.find_text(text):
            return
        else:
            self.back()


    def horiz_forward(self, steps=10):
        """
        向右滑动"
        :param steps: 步长
        :return: 
        """""
        self.d(scrollable=True).scroll.horiz.forward(steps=steps)


    def scroll_to_beginning(self, steps=10, max_swipes=1000):
        """
        向左滑动到开始位置
        :param steps: 步长
        :param max_swipes:
        :return:
        """
        self.d(scrollable=True).scroll.horiz.toBeginning(steps=steps, max_swipes=max_swipes)


    def sliding(self):
        '''向上滑动整屏'''
        self.d(scrollable=True).fling()


    def scroll_up(self, steps=10):
        """
        向上滑动
        :param steps: 步长
        :return:
        """
        self.d(scrollable=True).scroll(steps=steps)


    def swipe_up_screen(self,scre=1,steps=100):
        """
        向上滑动屏幕，默认为一屏
        :param scre: 滑动比例
        :return:
        """
        device_info = self.get_device_info()
        height = device_info.get("displayHeight")
        width = device_info.get("displayWidth")
        self.d.swipe(width / 2, height * 8 * scre / 9, width / 2, height / 9, steps=steps)


    def scro_text_click(self,position,scre=1,steps=3):
        '''
        根据文本滑动找到某个特定元素,并点击
        position:参数
        scre：滑动比例
        steps：步长
        '''
        device_info = self.get_device_info()
        height = device_info.get("displayHeight")
        width = device_info.get("displayWidth")
        self.d.swipe(width / 2, height * 8 * scre / 9, width / 2, height / 9, steps=steps)
        time.sleep(2)
        self.text_click(position)


    def vert_backward(self):
        """
        向下滑动
        :return:
        """
        self.d(scrollable=True).scroll.vert.backward()

    def swipes(self,sx, sy, ex, ey):
        """
        根据坐标滑动
        """
        self.d.swipe(sx, sy, ex, ey)

